<?php
include_once('inc/init.php');

$action=strFilter($_GET["action"]);

if($action=='getactive'){
	$actr=array('用户激活', '发送“用户激活”邮件', 'getActiveCode');
}elseif($action=='forgetpass'){
	$actr=array('重置密码', '发送“重置密码”邮件', 'getUserpass');	
}

$headtitle=$actr[0];
switch($action){

	//通过邮箱获取激活码
	case "getactive":
		if($cache_settings['issignupverify']!='2'){
			printMsg("public_noaction");
		}
		require_once('header.php');
		require_once getTemplatePath('public.htm');
		footer();
	break;

	//获取重置密码邮件
	case "forgetpass":
		require_once('header.php');
		require_once getTemplatePath('public.htm');
		footer();
	break;


	//激活用户
	case "active":
		$uid = numFilter($_GET["uid"]);
		$code = strFilter($_GET["code"]);
		if(empty($uid)||empty($code)){
			printMsg("public_active_err");
		}
		$d=$_SYS['time']-3*24*3600;
		$row=$db->row_select_one("userfield,users","users.id={$uid} and userfield.userid={$uid} and userfield.code='{$code}' and userfield.type=0 and userfield.createtime>{$d}","users.username as username, users.id as userid, users.groupid as groupid","users.id");
		if(empty($row)){
			printMsg("public_active_err");
		}else{
			if($row['groupid']!=GROUP_NOVERIFY){
				printMsg("public_active_err");
			}
			$user['groupid'] = 1;
			$db->row_update("users",$user,"id={$row['userid']}");
			$db->row_delete("userfield","userid={$row['userid']} and type=0");
			printMsg("public_active_succeed");
		}
	break;

	//重置密码
	case "resetpass":
		$uid = numFilter($_GET["uid"]);
		$code = strFilter($_GET["code"]);
		if(empty($uid)||empty($code)){
			printMsg("public_resetpass_err");
		}
		$d=$_SYS['time']-3*24*3600;
		$row=$db->row_select_one("userfield,users","users.id={$uid} and userfield.userid={$uid} and userfield.code='{$code}' and userfield.type=1 and userfield.createtime>{$d}","users.username as username, users.id as userid","users.id");
		if(empty($row)){
			printMsg("public_resetpass_err");
		}else{
			$newpass=mt_rand(100000,999999);
			$user['userpass'] = encrypt($row['username'],$newpass);
			$db->row_update("users",$user,"id={$row['userid']}");
			$db->row_delete("userfield","userid={$row['userid']} and type=1");
			printMsg("public_resetpass_succeed");
		}
	break;

	default:
		printMsg("public_noaction");
	break;
}
?>
